<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/10/28
 * Time: 10:27
 */

namespace app\adminapiv3\service;


class ServerAllowIp extends Base
{
    /**
     * @name 列表
     * @param $params
     * @param $limit
     * @return array
     * @author wx
     * @date 2019/10/28 10:36
     */
    function list($params, $limit)
    {
        $server = new \app\common\service\ServerAllowIp();
        $data = $server->list($params, $limit);

        //处理时间
        foreach($data['data'] as $k => $v){
            if($v['login_time']){
                $v['login_time'] = date('Y-m-d H:i:s',$v['login_time']);
            }else{
                $v['login_time'] = '暂未登录';
            }

            $data['data'][$k] = $v;
        }

        return $data;
    }

    /**
     * @name 添加
     * @param $addData
     * @return bool
     * @author wx
     * @date 2019/10/28 10:50
     */
    function add($addData)
    {
        //查看重复
        if ($this->checkExist(['ip' => $addData['ip']])) {
            $this->setError($this->CODE_FAIL, '请勿添加重复ip');
            return false;
        }

        $addData['login_time'] = 0;
        $addData['ctime'] = time();

        $server = new \app\common\service\ServerAllowIp();
        $server->add($addData);

        return true;
    }

    /**
     * @name 编辑
     * @param $id
     * @param $updateData
     * @return bool
     * @author wx
     * @date 2019/10/28 10:50
     */
    function modify($id, $updateData)
    {
        $server = new \app\common\service\ServerAllowIp();
        $server->modify(['id' => $id], $updateData);

        return true;
    }

    /**
     * @name 删除
     * @param $id
     * @return bool
     * @author wx
     * @date 2019/10/28 10:50
     */
    function del($id)
    {
        $condition[] = array('id', 'in', $id);

        $server = new \app\common\service\ServerAllowIp();
        $server->del($condition);

        return true;
    }

    function checkExist($condition)
    {
        $server = new \app\common\service\ServerAllowIp();
        $info = $server->findData($condition);

        if(!$info){
            return false;
        }

        return true;
    }

    /**
     * @name 功能设置
     * @author wx
     * @date 2021/1/7 10:06
     */
    public function setting($method = 'get', $params = array())
    {
        $fileName = '../runtime/config/allow_ip.conf';

        $dirName = '../runtime/config';
        if (!file_exists($dirName)) {
            mkdir($dirName, 0777);
        }

        if($method == 'get'){

            if(!file_exists($fileName)){

                $data = [
                    'time_range' => '0',
                    'status' => '0',
                ];
                file_put_contents($fileName, json_encode($data));
            }else{
                $data = json_decode(file_get_contents($fileName), true);
            }
        }else{
            $params['start_time'] = time();

            file_put_contents($fileName, json_encode($params));
            $data = $params;
        }

        return $data;
    }

}